package com.neu.listener.klz;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.io.*;

@WebListener()
public class VisitListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public VisitListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        String str="1000";
        try {
            File file=new File("d:\\text.txt");
            if(file.exists()){
                InputStream in=new FileInputStream(file);
                byte[] bytes=new byte[in.available()];
                int l=in.read(bytes);
                str=new String(bytes);
                in.close();
            }
            ServletContext application=sce.getServletContext();
            application.setAttribute("count",Integer.parseInt(str));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
        try {
            OutputStream out=new FileOutputStream("d:\\text.txt");
            ServletContext application=sce.getServletContext();
            Integer i= (Integer) application.getAttribute("count");
            out.write(i.toString().getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        ServletContext application=se.getSession().getServletContext();
        Integer num=(Integer)application.getAttribute("count");
        if(num==null){
            num=1200;
        }
        num++;
        application.setAttribute("count",num);
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}
